/**
 * Created by apple on 16/9/29.
 */
var slideIndex = 1;
var sideIndex = 1;
var beforeIndex = 1;
var _toady_total = 0;//今日更新的条数


$(document).ready(function () {
    //获取轮播图
    carousels();

    //焦点图
    var autoSlide = setInterval(mainSlide, 5000);
    $(".slide-list").find('li').click(function (event) {
        beforeIndex = slideIndex - 1;
        slideIndex = $(this).index();
        sideIndex = $(this).index();
        mainSlide();
    });

    $(".picture-wrapper").mouseenter(function (event) {
        clearInterval(autoSlide);
    });

    $(".picture-wrapper").mouseleave(function (event) {
        autoSlide = setInterval(mainSlide, 5000);
    });

    //获取所有主题
    themes();

    //今日更新
    today_update();
});

//点击各个分类展示(每日更新,之旅评测,摩友评测,等)
$(document).on('click', '.topic-label li', function () {
    $('.topic-label li').removeClass('active');
    $(this).addClass('active');
    $('#load-more-topic').show();
    if ($(this).hasClass('all')) {
        $('#picture-wrapper').show();
        $('#notice').show();
        $('.today').show();
        $('.banner').hide();
        $('.topicLabel-order').hide();
        $('.themes').hide();
        $('.topic-list-today').html('');
        $('.topic-list-older').html('');
        //今日更新
        today_update();
    } else {
        $('#picture-wrapper').hide();
        $('#notice').hide();
        $('.today').hide();
        $('.banner').show();
        $('.topicLabel-order').show();
        $('.themes').show();
        $('.order-button-area a').removeClass('current');
        $('.order-button-area a:first-child').addClass('current');
        theme_id = $(this).children('a').attr('id');
        page = 1;
        data_motocycle = {
            page: page,
            rows: 12,
            theme_id: theme_id
        };
        $('#topic_id_imgs').attr('src', '');
        $('.topic-list-themes').html('');

        //主题封面
        $.get(url_link + 'themes', function (data) {
            try {
                var obj = JSON.parse(data);
                //有数据列表
                if (obj.code == 0) {
                    //获取主题封面
                    $.each(obj.data, function (i, item) {
                        if (theme_id == item.theme_id) {
                            $('#topic_id_imgs').attr('src', item.cover)
                        }
                    });
                }

            } catch (e) {
            }
        });
        //文章列表
        article_list_index();

    }
});

//在各个分类下,不同的排序方式(最新发布,点击最高,评论最多)
$(document).on('click', '.order-button-area a', function () {
    $('#load-more-topic').show();
    $('.order-button-area a').removeClass('current');
    $(this).addClass('current');
    var m = $(this).html();
    theme_id = $('.topic-label').find('.active').children('a').attr('id');
    $('.topic-list-themes').html('');
    if (m == "最新发布") {
        page = 1;
        data_motocycle = {
            page: page,
            rows: 12,
            theme_id: theme_id
        };

    } else if (m == "点击最高") {
        page = 1;
        data_motocycle = {
            page: page,
            rows: 12,
            theme_id: theme_id,
            mostSee: 1
        };
    } else if (m == "评论最多") {
        page = 1;
        data_motocycle = {
            page: page,
            rows: 12,
            theme_id: theme_id,
            mostComment: 1
        };
    }
    article_list_index();
});

//首页的加载更多
$(document).on('click', '#load-more-topic', function () {
    page = page + 1;
    if (!data_motocycle.theme_id) {
        //首页
        data_motocycle = {
            page: page,
            rows: 18
        };
    } else {
        data_motocycle = {
            page: page,
            rows: 12,
            theme_id: theme_id
        };
    }
    article_list_index();
});


//获取轮播图
function carousels() {
    $.ajax({
        type: 'get',
        url: url_link + 'carousels',
        async: false,
        success: function (data) {
            try {
                var obj = JSON.parse(data);
                //轮播大图
                var carousels_img = "";
                var carousels_img_1 = "";
                //轮播小图
                var carousels_thumb = "";
                if (obj.code == 0) {
                    $.each(obj.data, function (i, item) {
                        carousels_img += "<li>" +
                            "<a href='html/publish/show.html?article_id=" + item.article_id + "' class='img-box' target='_blank'>";
                        if (i == 0) {
                            carousels_thumb += "<li style='margin-top:0;' class='active'>"
                        } else {
                            carousels_thumb += "<li>"
                        }
                        carousels_img += "<img src='image/base/none_img.png' data-url=" + item.cover + ">" +
                            "</a>" +
                            "</li>";
                        carousels_thumb += "<a>" +
                            "<img src='image/base/none_img.png' data-url=" + item.cover + ">" +
                            "<em></em>" +
                            "<span><b>" + item.articleVo.title + "</b></span></a>" +
                            "</li>";
                        //轮播大图要多一张
                        if (i == 0) {
                            carousels_img_1 += "<li>" +
                                "<a href='html/publish/show.html?article_id=" + item.article_id + "' class='img-box' target='_blank'>" +
                                "<img src='" + item.cover + "' alt=" + item.articleVo.title + " data-url=" + item.cover + ">" +
                                "</a>" +
                                "</li>";
                        }
                    });
                    //轮播图内数量不够
                    if (obj.data.length < 5) {
                        var _p = 5 - obj.data.length;
                        for (var _q = 0; _q < _p; _q++) {
                            carousels_img += "<li><a class='img-box' target='_blank'>";
                            if (_p == 5) {
                                carousels_thumb += "<li style='margin-top:0;' class='active'>"
                            } else {
                                carousels_thumb += "<li>"
                            }
                            carousels_thumb += "<em></em><a><img src='image/index/test_" + (_q + 1) + ".jpg'><span><b></b></span></a></li>";
                            carousels_img += "<img src='image/index/test_" + (_q + 1) + ".jpg'><span></span></a></li>";

                            //轮播大图要多一张
                            if (_p == 5) {
                                carousels_img_1 += "<li><a class='img-box' target='_blank'><img src='image/index/test_" + (_q + 1) + ".jpg'><span></span></a></li>";
                            }
                        }
                    }
                    $('.slide-main ul').html(carousels_img);
                    $('.slide-main ul').append(carousels_img_1);
                    $('.slide-list').html(carousels_thumb);

                    //l轮播大图展示
                    setTimeout(function(){
                        $('.slide-main ul img').each(function(){
                            var _data_url=$(this).attr('data-url');
                            $(this).attr('src',_data_url);
                        });
                    },2000);
                    //右侧相框小图展示
                    setTimeout(function(){
                        $('.slide-list img').each(function(){
                            var _data_url=$(this).attr('data-url');
                            $(this).attr('src',_data_url);
                        })
                    },500)



                }
            } catch (e) {
            }
        }
    });
}

//焦点图切换
function mainSlide() {
    $(".slide-main ul").find('li').css({'z-index': '0'});
    $(".slide-list").find('li').removeClass('active');
    $(".slide-list").find('li:eq(' + sideIndex + ')').addClass('active');
    $(".slide-main ul").find('li:eq(' + slideIndex + ')').css({
        'z-index': '5',
        'display': 'none'
    });
    $(".slide-main ul").find('li:eq(' + beforeIndex + ')').css({
        'z-index': '4'
    });
    $(".slide-main ul").find('li:eq(' + slideIndex + ')').fadeIn(800);
    slideIndex = slideIndex + 1;
    sideIndex = sideIndex + 1;
    beforeIndex = slideIndex - 1;
    if (sideIndex > 4) {
        sideIndex = 0;
    }
    if (slideIndex > 5) {
        slideIndex = 1;
    }
}

//获取所有主题
function themes() {
    $.get(url_link + 'themes', {}, function (data) {
        try {
            var obj = JSON.parse(data);
            var themes_info = '';
            //有数据列表
            if (obj.code == 0) {
                themes_info += "<li class='active all'><a>每日更新</a></li>";
                $.each(obj.data, function (i, item) {
                    themes_info += "<li><a id=" + item.theme_id + ">" + item.name + "</a></li>";
                });
                $('.topic-label').html(themes_info)
            }
        } catch (e) {
        }
    })
}

//今日更新
function today_update() {
    data_motocycle = {
        page: 1,
        rows: 18
    };
    article_list_index();
}

//文章列表
function article_list_index() {
    $.ajax({
        url: url_link + 'articles',
        data: data_motocycle,
        async: false,
        success: function (data) {
            var obj = JSON.parse(data);
            var topic_list = "";
            var _num = 0;
            var _data_id = 0;
            var _today = false;
            if (obj.code == 0) {
                if (obj.data.rows.length != 0) {
                    if (obj.data.rows.length < data_motocycle.rows) {
                        $('#load-more-topic').hide();
                    }
                    $.each(obj.data.rows, function (i, item) {
                        _data_id = i + 1;
                        //判断是否是今日更新
                        var updated_time = item.updatedDate.substring(0, 10);
                        if (mytime == updated_time) {
                            item.updatedDate = "今天   " + item.updatedDate.substring(11, item.updatedDate.length)
                        } else {
                            _num += 1;
                        }
                        if (_num == 1 && data_motocycle.page == 1 && i < 6 && !data_motocycle.theme_id) {
                            //本日消息不足6条
                            if (_today == false) {
                                if (i != 0) {
                                    _today = true;
                                    topic_list += "</ul><div style='clear: both'></div>" +
                                        "<ul class='topic-list topic_first'>" +
                                        "<p style='margin: 0 12px;color: black;'>往夕精彩</p>" +
                                        "</ul>" +
                                        "<div style='clear: both'></div>" +
                                        "<ul class='topic-list'>"
                                } else {
                                    topic_list += "<div style='clear: both'></div>" +
                                        "<ul class='topic-list topic_first'>" +
                                        "<p style='margin: 0 12px;color: black;'>往夕精彩</p>" +
                                        "</ul>" +
                                        "<div style='clear: both'></div>" +
                                        "<ul class='topic-list'>"
                                }
                            }

                        } else if (_num == 0 && data_motocycle.page == 1 && i == 6 && !data_motocycle.theme_id) {
                            //本日消息有6条或以上
                            topic_list += "</ul>" +
                                "<div style='clear: both'></div>" +
                                "<ul class='topic-list topic_first'>" +
                                "<p style='margin: 0 12px;color: black;'>往夕精彩</p>" +
                                "</ul>" +
                                "<div style='clear: both'></div>" +
                                "<ul class='topic-list'>"
                        }
                        if (mytime == updated_time && i < 6) {
                            topic_list += "<li class='today' data-id=" + _data_id + " data-page=" + data_motocycle.page + ">";
                            _toady_total += 1;
                        } else {
                            topic_list += "<li data-id=" + _data_id + " data-page=" + data_motocycle.page + ">";
                        }

                        topic_list += "<a href='html/publish/show.html?article_id=" + item.article_id + "' class='img-box' target='_blank'>";

                        if (item.cover) {
                            topic_list += "<img src='image/base/none_img.png' data-url=" + item.cover + ">";
                        } else {
                            var _content = item.content.split("$$##MotorTravel##$$");
                            var _count = 0;
                            for (var _a = 0; _a < _content.length; _a++) {
                                var m = _content[_a].substring(0, 13);
                                if (m == "$$##Image##$$") {
                                    //有图
                                    _count = 1;
                                    var _content_image_description = _content[_a].split("$$##ImageDescription##$$");
                                    var _content_image_url = _content_image_description[0].split("$$##ImageInfo##$$");
                                    topic_list += "<img src='image/base/none_img.png' data-url=" + _content_image_url[0].substring(13, _content_image_url[0].length) + ">";
                                    break;
                                }
                            }
                            //纯文字,没有图的情况下,使用备用图片
                            if (_count == 0) {
                                topic_list += "<img src='image/base/none_img.png' data-url='image/base/none_img.png'>"
                            }
                        }


                        topic_list += "</a>" +
                            "<span class='topic-info-box'>" +
                            "<a href='html/discover_theme.html?mototype_id=" + item.mototype_id + "' class='topic-lab' data_mototype_id=" + item.mototype_id + ">" + item.mototype_name + "</a>";
                        topic_list += "<em class='topic-pub-time'>" + item.updatedDate + "</em>" +
                            "</span>" +
                            "<h3 class='topic-title'>";
                        if (item.is_nice == 1) {
                            topic_list += "<i class='essence'></i>"
                        }
                        topic_list += "<a href='html/publish/show.html?article_id=" + item.article_id + "' target='_blank'>" + item.title + "</a></h3>" +
                            "<div class='topic-user-info'>" +
                            "<a href='html/lookat/article.html?user_id=" + item.user_id + "' class='user-info'>"
                        if (item.user.avatar) {
                            topic_list += "<i><img src=" + item.user.avatar + " style='width: 25px;height: 25px;'></i>"
                        } else {
                            topic_list += "<i><img src='image/base/none.png' style='width: 25px;height: 25px;'></i>"
                        }
                        topic_list += "<span>" + item.user.nickname + "</span>" +
                            "</a>" +
                            "<div class='about-count'>" +
                            "<span class='click-count'><i></i><b>" + item.see_count + "</b></span>" +
                            "<span class='comment-count'><i></i><b>" + item.comment_count + "</b></span>" +
                            "<span class='praise-count'><i></i><b>" + item.gift_count + "</b></span>" +
                            "</div>" +
                            "</div>" +
                            "</li>";

                    });
                    var _remainder = (data_motocycle.rows - _toady_total) % 3;
                    if (!data_motocycle.theme_id) {
                        //今日更新
                        if ($('.topic-list-today li').length == 0) {
                            $('.topic-list-today').html(topic_list);
                        } else {
                            $('.topic-list-today').append(topic_list);
                        }
                        $('.topic-list-today li').show();
                        $('.topic-list-today li').each(function () {
                            var _data_id = $(this).attr('data-id');
                            var _data_page = $(this).attr('data-page');
                            if (obj.data.rows.length == data_motocycle.rows) {
                                if (_remainder == 1 && _data_page == data_motocycle.page && _data_id == data_motocycle.rows) {
                                    //当前页面下,多一条数据
                                    $(this).hide();
                                } else if (_remainder == 2 && _data_page == data_motocycle.page && _data_id == data_motocycle.rows) {
                                    //当前页面下,多两条数据
                                    $(this).hide();
                                } else if (_remainder == 2 && _data_page == data_motocycle.page && _data_id == data_motocycle.rows - 1) {
                                    //当前页面下,多两条数据
                                    $(this).hide();
                                }
                            }
                        });

                        //更换首页封面图片
                        setTimeout(function(){
                            change_url($('.topic-list-today img'));
                        },500);
                    } else {
                        if (data_motocycle.mostSee == 1) {
                            //点击各个分类展示(排序————————点击最高)
                            $('.topicLabel-order').children('span').html("共" + obj.data.total + "篇帖子");
                            if ($('.topic-list-themes li').length == 0) {
                                $('.topic-list-themes').html(topic_list);
                            } else {
                                $('.topic-list-themes').append(topic_list);
                            }
                        } else if (data_motocycle.mostComment == 1) {
                            //点击各个分类展示(排序————————评论最多)
                            $('.topicLabel-order').children('span').html("共" + obj.data.total + "篇帖子");
                            if ($('.topic-list-themes li').length == 0) {
                                $('.topic-list-themes').html(topic_list);
                            } else {
                                $('.topic-list-themes').append(topic_list);
                            }
                        } else {
                            //点击各个分类展示(排序————————最新发布)
                            $('.topicLabel-order').children('span').html("共" + obj.data.total + "篇帖子");
                            if ($('.topic-list-themes li').length == 0) {
                                $('.topic-list-themes').html(topic_list);
                            } else {
                                $('.topic-list-themes').append(topic_list);
                            }
                        }
                        //更换具体分类主题里面的文章图片
                        setTimeout(function(){
                            change_url($('.topic-list-themes img'));
                        },500);
                    }
                    //主题颜色
                    topicColor();

                } else {
                    $('#load-more-topic').hide();
                    if (!data_motocycle.theme_id) {
                        //今日更新
                        if ($('.topic-list-today li').length == 0) {
                            $('.topic-list-themes').html("<h2 class='none_info'>暂时还没有相关文章</h2>");
                        } else {
                            alert('没有内容啦!去看看别的吧!');
                        }
                    } else {
                        if (data_motocycle.mostSee == 1) {
                            //点击各个分类展示(排序————————点击最高)
                            //共几篇帖子,数量递增
                            if ($('.topic-list-themes li').length == 0) {
                                $('.topicLabel-order').children('span').html("共" + obj.data.total + "篇帖子");
                                $('.topic-list-themes').html("<h2 class='none_info'>暂时还没有相关文章</h2>");
                            } else {
                                alert('没有内容啦!去看看别的吧!');
                            }
                        } else if (data_motocycle.mostComment == 1) {
                            //点击各个分类展示(排序————————评论最多)
                            //共几篇帖子,数量递增
                            if ($('.topic-list-themes li').length == 0) {
                                $('.topicLabel-order').children('span').html("共" + obj.data.total + "篇帖子");
                                $('.topic-list-themes').html("<h2 class='none_info'>暂时还没有相关文章</h2>");
                            } else {
                                alert('没有内容啦!去看看别的吧!');
                            }
                        } else {
                            //点击各个分类展示(排序————————最新发布)
                            //共几篇帖子,数量递增
                            if ($('.topic-list-themes li').length == 0) {
                                $('.topicLabel-order').children('span').html("共" + obj.data.total + "篇帖子");
                                $('.topic-list-themes').html("<h2 class='none_info'>暂时还没有相关文章</h2>");
                            } else {
                                alert('没有内容啦!去看看别的吧!');
                            }
                        }
                    }
                }
            }
            page = data_motocycle.page;
        }
    });
}

//获取文章封面图
function change_url(e){
    e.each(function(){
        var _data_url=$(this).attr('data-url');
        if(_data_url){
            if(_data_url=="image/base/none_img.png"){
                $(this).attr('src',_data_url);
            }else{
                if(_data_url.substring(_data_url.length-2,_data_url.length)=="80"){
                    //web上传的图片
                    $(this).attr('src',_data_url+'/rotate/auto');
                }else{
                    $(this).attr('src',_data_url+'!/rotate/auto');
                }

            }
            $(this).attr('data-url','')
        }

    })
}